#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e7;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL primes[maxn],tot=0;
LL Maxv[maxn];
LL v[maxn];
void getprimes(LL n){

    for(LL i=2;i<=n;i++){
        if(v[i]==0){v[i]=i;primes[++tot]=i;Maxv[i]=i;}
        for(LL j=1;j<=tot&&primes[j]<=n/i;j++){
            v[i*primes[j]]=primes[j];
            Maxv[i*primes[j]]=max(primes[j],Maxv[i]);
            if(i%primes[j]==0) break;
        }
    }
}
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL X2;cin>>X2;
  getprimes(X2);
  if(v[X2]==0){
    cout<<X2<<"\n";
  }
  else{
    LL ans=0x3f3f3f3f;
    for(LL i=X2-Maxv[X2]+1;i<=X2;i++){
        if(v[i]!=i) ans=min(ans,i-Maxv[i]+1);
    }
  cout<<ans<<"\n";
  }
return 0;
}
